package com.imooc.distributedemo.resume.leetcode;

import java.util.BitSet;
import java.util.Iterator;
import java.util.Stack;

/**
 * leetcode136 只出现1次的数字
 */
public class Solution136 {
    public static void main(String[] args) {
        int[] nums = {4,1,2,1,2};

        int target2 = singleNumber2(nums);
        System.out.println(target2);

        int target = singleNumber(nums);
        System.out.println(target);
    }

    public static int singleNumber(int[] nums) {
        BitSet set = new BitSet();
        return 0;
    }

    public static int singleNumber2(int[] nums) {
        if(nums==null ||nums.length<1){
            return 0;
        }
        if(nums.length==1){
            return nums[0];
        }
        Stack<Character> stack = new Stack<>();
        for (int num:nums){
            if (!stack.isEmpty() && stack.contains((char)num)) {
                Iterator<Character> iterator = stack.iterator();
                while (iterator.hasNext()) {
                    int element = iterator.next();
                    if (element == num) {
                        iterator.remove();
                        break; // 如果只想移除第一个匹配的元素，则使用break跳出循环
                    }
                }
            }else{
                stack.push((char)num);
            }
        }
        if(stack.size()!=0){
            return (int)stack.pop();
        }
        return 0;
        /*int result = 0;
        for(int num:nums){
            result ^= num;
        }

        int r1 = 0^4;
        int r2 = 4^0^4;
        return result;*/
    }
}
